#!/usr/bin/python
#coding=utf-8

'''
'''

import os
import sys
curdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(curdir)
# sys.path.insert(0, '..') 上层目录
sys.path.insert(0, os.path.dirname(os.path.normpath(curdir)))
import pandas as pd
import numpy as np
import platform
from time import time

from statser.StatisticsReporter import sql_StatisticsReporter, df_StatisticsReporter, stream_StatisticsReporter, scriptError
from statser import Dict2Html
from statser import CommonUtils
from statser import SR_Utils
from statser import SQL_Utils
from statser import StrUtils
from statser.StreamRegularDict import StreamWebDict, StreamDataframeDict
from statser import WebHelper
from statser import MiscUtils
from statser import sample_data
from statser.SR_Thread import df_SR_Thread_Go, stream_SR_Thread_Go, sql_SR_Thread_Go
from statser.SR_MaterSlave import *

logger = CommonUtils.getLogger('sample')

if __name__ == '__main__':
    sysstr = platform.system()
    curpath = os.path.dirname(os.path.abspath(__file__))

    if (len(sys.argv) >= 2):
        argv1 = int(sys.argv[1])
    else:
        argv1 = int(0)

    if (True and argv1 in (0,11)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_y_dynamic_record.sr'
        else:
            grammer = curpath + '\\docs\\demo_y_dynamic_record.sr'

        if 0:
            demo_df_ = []
            for i in range(4):
                df_ = sample_data.demo_df(10)
                demo_df_.append(df_)
        else:
            demo_df_ = sample_data.demo_df(10)

        #ctl_Master(True)
        #ctl_Master_cfg(True, '')

        _src = None
        start = time()
        #status, res, _src = df_SR_Master_Go(grammer, _src, {}, demo_df_, nslaves=0)
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))
        scriptError(_src.grammer, sys._getframe().f_lineno, 'sample', logger_error=False)

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))
            retStr = Dict2Html.dict2Html(_src, src_moredata='all', valNone='?', fmtYtitle=1)
            print (retStr)
            for i in range(10):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = df_SR_Master_Go(grammer, _src0, {}, demo_df_, nslaves=0)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))

    if (True and argv1 in (0,10)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_y_dynamic.sr'
        else:
            grammer = curpath + '\\docs\\demo_y_dynamic.sr'

        if 0:
            demo_df_ = []
            for i in range(4):
                df_ = sample_data.demo_df(10000)
                demo_df_.append(df_)
        else:
            demo_df_ = sample_data.demo_df(10000)

        #ctl_Master_cfg(True, '')

        _src = None
        start = time()
        #status, res, _src = df_SR_Master_Go(grammer, _src, {}, demo_df_, nslaves=0)
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))
            for i in range(10):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = df_SR_Master_Go(grammer, _src0, {}, demo_df_, nslaves=0)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))

    if (True and argv1 in (0,9)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_df_sample.sr'
        else:
            grammer = curpath + '\\docs\\demo_df_sample.sr'
        demo_df_ = sample_data.demo_df()
        _src = None
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_sql_sample_pearsonr.sr'
        else:
            grammer = curpath + '\\docs\\demo_sql_sample_pearsonr.sr'

        conn = SQL_Utils.getConn(host='192.168.1.110', user='root', password='Elastos182', dbname='op_einvoicedb', dictResult=False)
        conn_pool = SQL_Utils.getDbpool(host='192.168.1.110', user='root', password='Elastos182', dbname='op_einvoicedb', dictResult=False)

        # 初始化并发环境
        ctl_Master_cfg(True, '')
        ctl_ConnPool(conn_pool)

        start = time()
        #status, res, _src = sql_SR_Master_Go(grammer, _src, {}, None, dictResult=False, limitRows=10000, nslaves=4)
        status, res, _src = sql_StatisticsReporter(grammer, _src, {}, conn, dictResult=False, debug=False, dump=True)
        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            print ('程序运行出错！')
        else:
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))

            for i in range(10):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = sql_SR_Master_Go(grammer, _src0, {}, conn_pool, dictResult=False, limitRows=10000)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))

    if (True and argv1 in (0,8)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_df_sample.sr'
        else:
            grammer = curpath + '\\docs\\demo_df_sample.sr'
        demo_df_ = sample_data.demo_df()
        _src = None
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_sql_sample_pearsonr.sr'
        else:
            grammer = curpath + '\\docs\\demo_sql_sample_pearsonr.sr'

        #conn = SQL_Utils.getConn(host='192.168.1.110', user='root', password='Elastos182', dbname='op_einvoicedb', dictResult=False)
        conn_pool = SQL_Utils.getDbpool(host='192.168.1.110', user='root', password='Elastos182', dbname='op_einvoicedb', dictResult=False)
        start = time()
        status, res, _src = sql_SR_Thread_Go(grammer, _src, {}, conn_pool, dictResult=False, limitRows=10000)
        #status, res, _src = sql_StatisticsReporter(grammer, _src, {}, conn, dictResult=False, debug=False, dump=True)
        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            print ('程序运行出错！')
        else:
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))

            for i in range(10):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = sql_SR_Thread_Go(grammer, _src0, {}, conn_pool, dictResult=False, limitRows=10000)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))

    if (True and argv1 in (0,7)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_df_sample_y_dict.sr'
        else:
            grammer = curpath + '\\docs\\demo_df_sample_y_dict.sr'

        demo_df_ = sample_data.demo_df(10000)
        sdd = StreamDataframeDict(demo_df_)

        _src = None
        start = time()
        #status, res, _src = stream_StatisticsReporter(grammer, _src, {}, sdd)
        status, res, _src = stream_SR_Thread_Go(grammer, _src, {}, sdd)
        #scriptError(_src.grammer, sys._getframe().f_lineno)

        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno)
        else:
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))

    if (True and argv1 in (0,6)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_y_dynamic.sr'
        else:
            grammer = curpath + '\\docs\\demo_y_dynamic.sr'

        if 0:
            demo_df_ = []
            for i in range(4):
                df_ = sample_data.demo_df(10000)
                demo_df_.append(df_)
        else:
            demo_df_ = sample_data.demo_df(10000)

        _src = None
        start = time()
        #(sr_filename, _src=None, parameters={}, df=None, fun_require=deal_require, npartitions=1):
        status, res, _src = df_SR_Thread_Go(grammer, _src, {}, demo_df_, npartitions=0)
        #status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')

        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))
            for i in range(1):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = df_SR_Thread_Go(grammer, _src0, {}, demo_df_, npartitions=0)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))

    if (True and argv1 in (0,5)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_df_sample_y_dict.sr'
        else:
            grammer = curpath + '\\docs\\demo_df_sample_y_dict.sr'

        if 0:
            demo_df_ = []
            for i in range(4):
                df_ = sample_data.demo_df(10000)
                demo_df_.append(df_)
        else:
            demo_df_ = sample_data.demo_df(10000)

        _src = None
        start = time()
        #(sr_filename, _src=None, parameters={}, df=None, fun_require=deal_require, npartitions=1):
        status, res, _src = df_SR_Thread_Go(grammer, _src, {}, demo_df_, npartitions=0)
        #scriptError(_src.grammer, sys._getframe().f_lineno)

        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            for i in range(1):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = df_SR_Thread_Go(grammer, _src0, {}, demo_df_, npartitions=0)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))

    if (True and argv1 in (0,1)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_df_sample.sr'
        else:
            grammer = curpath + '\\docs\\demo_df_sample.sr'
        demo_df_ = sample_data.demo_df()
        _src = None
        '''
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        print (status, res, _src)
        '''
        #if (status != 0):
        '''
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        print (status, res, _src)

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno)
        else:
            retStr = Dict2Html.dict2Html(_src)
            print (retStr)

            df = SR_Utils.getResultDf(_src)
            pd.set_option('display.max_columns', None)
            print (df.head())

        '''
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_sql_sample_pearsonr.sr'
        else:
            grammer = curpath + '\\docs\\demo_sql_sample_pearsonr.sr'

        conn = SQL_Utils.getConn(host='192.168.1.110', user='root', password='Elastos182', dbname='op_einvoicedb', dictResult=False)
        start = time()
        #status, res, _src = sql_StatisticsReporter(grammer, _src, {}, conn, False, '192.168.1.110', 'root', 'Elastos182', 'op_einvoicedb', {}, '', debug=False, dump=True)
        status, res, _src = sql_StatisticsReporter(grammer, _src, {}, conn, dictResult=False, debug=False, dump=True)
        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            print ('程序运行出错！')
        else:
            for i in range(10):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = sql_StatisticsReporter(grammer, _src0, {}, conn, dictResult=False, debug=False)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))
            '''
            print (str(res))
            print (CommonUtils.getTitles(_src))
            retStr = Dict2Html.dict2Html(_src)
            print (retStr)
            '''
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            print(df.head(50))

    if (True and argv1 in (0,2)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_df_sample_y_dict.sr'
        else:
            grammer = curpath + '\\docs\\demo_df_sample_y_dict.sr'
        demo_df_ = sample_data.demo_df()
        _src = None
        '''
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        print (status, res, _src)
        '''
        #if (status != 0):
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        print (status, res, _src)

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            retStr = SR_Utils.getJsonForEchart(_src)
            print ('---------\n', retStr)

            retStr = Dict2Html.dict2Html(_src)
            print (retStr)

            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False)
            pd.set_option('display.max_columns', None)
            print (df.head())

    if (True and argv1 in (0,3)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/test_template_percentile.sr'
        else:
            grammer = curpath + '\\docs\\test_template_percentile.sr'
        demo_df_ = sample_data.demo_df()
        _src = None
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, demo_df_, debug=False)
        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            retStr = SR_Utils.getJsonForEchart(_src)
            print ('---------\n', retStr)

            retStr = Dict2Html.dict2Html(_src, src_moredata=True)
            print (retStr)

            retStr = MiscUtils.moredata2Json(_src)
            print ('\n__________MiscUtils.moredata2Json(_src)__________\n', retStr)

            retStr = MiscUtils.dictdata2Json(_src)
            print ('\n__________MiscUtils.dictdata2Json(_src)__________\n', retStr)

            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False, keepIndexInData=False)
            pd.set_option('display.max_columns', None)
            print (df.head(20))

    # sample for: StreamWebDict
    if (True and argv1 in (0,4)):
        if (sysstr == "Linux"):
            grammer = curpath + '/docs/demo_sql_sample_pearsonr.sr'
        else:
            grammer = curpath + '\\docs\\demo_one_statement_more_lines.sr'
        demo_df_ = sample_data.demo_df()
        _src = None

        obj = WebHelper.WebHelper('http://www.brthm.com', 'dataisland', "dataisland")

        df_vehicles_68000 = sample_data.load_vehicles()
        demo_df_ = sample_data.demo_df()
        start = time()
        status, res, _src = df_StatisticsReporter(grammer, _src, {}, df_vehicles_68000, debug=False, fun_require=WebHelper.deal_require_dict, dump=True)
        finish = time()
        print ("-1-----> {:10.6}s".format(finish - start))

        if status:
            scriptError(_src.grammer, sys._getframe().f_lineno, 'sample')
        else:
            for i in range(10):
                _src0 = SR_Utils.src_Load(_src.dump)
                start = time()
                status, res, _src1 = df_StatisticsReporter(grammer, _src0, {}, df_vehicles_68000, debug=False, fun_require=WebHelper.deal_require_dict, dump=False)
                finish = time()
                print ("-2-----> {:10.6}s".format(finish - start))

            '''
            retStr = SR_Utils.getJsonForEchart(_src)
            print ('---------\n', retStr)

            retStr = Dict2Html.dict2Html(_src, src_moredata=False)
            print (retStr)

            retStr = MiscUtils.moredata2Json(_src)
            print ('\n__________MiscUtils.moredata2Json(_src)__________\n', retStr)

            retStr = MiscUtils.dictdata2Json(_src)
            print ('\n__________MiscUtils.dictdata2Json(_src)__________\n', retStr)
            '''
            df = SR_Utils.getResultDf(_src, xMultiIndex=False, yMultiIndex=False, keepIndexInData=False)
            pd.set_option('display.max_columns', None)
            print (df.head(20))

